import java.io.*;
/*****************************************************************************
 * Assertive Speech over Sockets Graphical user interface.
 * This form was generated using NetBeans IDE 7.0.1
 *
 * End user application for ASSA distributed application.
 * 
 * It is concieved only as a prototype for an end user application and
 * not a usable product.  The code is not for sale and may not be
 * redistributed with the intent of sale, as I do not own its content.
 * Therefore, documentation of its content is trivial as well.
 *****************************************************************************/
public class ASSGui extends javax.swing.JFrame {

	private final ASSClientLink link;
	private final PipedInputStream pin;
	private final PipedOutputStream pout;

	ASSGui(){
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e) {
			;// YOU NEED TO DO NOTHING
        }
		initComponents();
		/* Only way to make an inputstream is to wrap an output stream in pipedinputstream */
		PipedInputStream in=null;
		PipedOutputStream out=null;
		ASSClientLink c=null;
		try{
			in = new PipedInputStream();
			out = new PipedOutputStream(in);
			c=new ASSClientLink(in,(new PrintStream(new GOutputStream(outbox))),false);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		pin=in;
		pout=out;
		link=c;
		link.help();
		refresh();
	}// Constructor
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        outbox = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        inbox = new javax.swing.JTextArea();
        auto = new javax.swing.JLabel();
        send = new javax.swing.JButton();
        help = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Assertive Speech over Sockets Gui Chat");
        setBackground(new java.awt.Color(0, 102, 102));
        setForeground(new java.awt.Color(0, 51, 51));
        setIconImages(null);
		this.setIconImage(new javax.swing.ImageIcon(getClass().getResource("asinus1.gif")).getImage()); 
        setResizable(false);

        jScrollPane1.setBackground(new java.awt.Color(51, 51, 51));
        jScrollPane1.setAutoscrolls(true);

        outbox.setBackground(new java.awt.Color(0, 0, 0));
        outbox.setColumns(50);
        outbox.setEditable(false);
        outbox.setFont(new java.awt.Font("Lucida Console", 0, 12)); // NOI18N
        outbox.setForeground(new java.awt.Color(0, 255, 51));
        outbox.setLineWrap(true);
        outbox.setRows(5);
        outbox.setWrapStyleWord(true);
        jScrollPane1.setViewportView(outbox);

        jScrollPane2.setBackground(new java.awt.Color(102, 102, 102));
        jScrollPane2.setAutoscrolls(true);

        inbox.setBackground(new java.awt.Color(0, 0, 0));
        inbox.setColumns(20);
        inbox.setFont(new java.awt.Font("Lucida Console", 0, 14)); // NOI18N
        inbox.setForeground(new java.awt.Color(255, 0, 255));
        inbox.setLineWrap(true);
        inbox.setRows(5);
        inbox.setWrapStyleWord(true);
        inbox.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                inboxKeyReleased(evt);
            }
        });
        jScrollPane2.setViewportView(inbox);

        auto.setBackground(new java.awt.Color(0, 0, 0));
        auto.setFont(new java.awt.Font("Lucida Console", 0, 14)); // NOI18N
        auto.setForeground(new java.awt.Color(102, 0, 255));
        auto.setText("");

        send.setText("Send");
        send.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                sendMouseReleased(evt);
            }
        });

        help.setText("Help");
        help.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                helpMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(auto, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
                        .addComponent(help)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(send)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(send)
                    .addComponent(help)
                    .addComponent(auto))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

	 /* Model the widget as providing output to a process when the keypress enter
	  * pressing enter causes stupid actionlistener in real program to interrupt this
	  */
	private void inboxKeyReleased(java.awt.event.KeyEvent evt) {
		/* Keycode is natural equivalent to ascii
		 * 10=enter
		 */
    	if(evt.getKeyCode()==10){
			String s=inbox.getText();//get String from box
			s=s.replace("\r","");
			s=s.replace("\n","");
			inbox.setText("");
			s=s+"\r\n";
			try{
				pout.write(s.getBytes(),0,s.length()-1);
			}
			catch(Exception e){
				e.printStackTrace();
			}
			System.out.println("processing string="+s);
			refresh();
		}
		auto.setText(link.autoCommand+" "+link.autoUid);
	}/*</inboxKeyReleased>*/
	
	private void sendMouseReleased(java.awt.event.MouseEvent evt) {
		String s=inbox.getText();//get String from box
		s=s.replace("\r","");
		s=s.replace("\n","");
		s=s+"\r\n";
		try{
			pout.write(s.getBytes(),0,s.length()-1);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		System.out.println("String processed="+s);
		refresh();
	}
	
	private void helpMouseReleased(java.awt.event.MouseEvent evt) {
		link.help();
		refresh();
	}
	
	private synchronized void refresh(){
		inbox.setText("");		
		inbox.requestFocusInWindow();
		auto.setText(link.autoCommand+" "+link.autoUid);
	}

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ASSGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ASSGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ASSGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ASSGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ASSGui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JLabel auto;
    private javax.swing.JButton help;
    private javax.swing.JTextArea inbox;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea outbox;
    private javax.swing.JButton send;
    // End of variables declaration
}
